package com.test.ipc.socket;

import com.test.ipc.Logger;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Parent {
    private static final String TAG = "Socket.Parent";
    private static DataOutputStream mDataOutputStream = null;
    private static DataInputStream mDataInputStream = null;
    private static Socket mClientSocket = null;
    private static ServerSocket mServerSocket = null;

    public static void main(String[] args) {

        Process mProcess = null;
        Thread mReadThread;
        Thread mWriteThread;

        int port;
        try {
            mServerSocket = new ServerSocket(0);
            port = mServerSocket.getLocalPort();
        } catch (Exception e) {
            port = 9923;
        }
        Logger.d(TAG+",服务端的端口号："+port);


        try {
            mProcess = Runtime.getRuntime().exec("java com.test.ipc.socket.Child -p " + port);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(TAG+","+e.getMessage());
        }
        if (mProcess == null) {
            return;
        }


        mWriteThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    i++;
                    try {
                        String str = ""+i+"\r\n";
                        Logger.d(TAG+",Send: " + str);
                        mDataOutputStream.write(str.getBytes());
                        mDataOutputStream.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                        Logger.e(TAG+","+e.getMessage());
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Logger.e(TAG+","+e.getMessage());
                    }
                }
            }
        });


        mReadThread = new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bfr = new BufferedReader(new InputStreamReader(mDataInputStream));
                while (true) {
                    try {
                        String rd = bfr.readLine();
                        if (rd != null) {
                            Logger.d(TAG+",Rcv: " + rd);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Logger.e(TAG+","+e.getMessage());
                        break;
                    }
                }
            }
        });


        try {
            mClientSocket = mServerSocket.accept();
            Logger.d("客户端的端口号："+ mClientSocket.getPort());
            mDataInputStream = new DataInputStream(mClientSocket.getInputStream());
            mDataOutputStream = new DataOutputStream(mClientSocket.getOutputStream());

            mWriteThread.start();
            mReadThread.start();
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(TAG+","+e.getMessage());
        }


        try {
            mReadThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            mWriteThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            mDataInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            mDataOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            mServerSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mClientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
